package com.bwie.service.impl;

import cn.hutool.core.util.RandomUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.bwie.mapper.TbRoleMapper;
import com.bwie.pojo.TbRole;
import com.bwie.pojo.TbUser;
import com.bwie.service.TbUserService;
import com.bwie.mapper.TbUserMapper;
import com.bwie.utils.MobileMessageUtil;
import com.bwie.utils.R;
import com.bwie.vo.LoginInfoVo;
import com.bwie.vo.PageInfoVo;
import io.jsonwebtoken.Jwts;
import io.jsonwebtoken.SignatureAlgorithm;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;

import java.util.concurrent.TimeUnit;

/**
* @author 18229
* @description 针对表【tb_user】的数据库操作Service实现
* @createDate 2025-04-22 19:48:56
*/
@Service
public class TbUserServiceImpl extends ServiceImpl<TbUserMapper, TbUser>
        implements TbUserService {

    @Autowired
    StringRedisTemplate stringRedisTemplate;
    @Autowired
    TbUserMapper tbUserMapper;




    @Override
    public R sendCode(String phone) {

        //使用mapper层判断手机号是否存在
        TbUser tbUser = tbUserMapper.selectOne(new QueryWrapper<TbUser>().lambda().eq(TbUser::getPhone, phone));
        if (tbUser==null){
            return R.ERROR(500,"手机号不存在");
        }

        //生成4位数短信验证码
        String phoneCode = RandomUtil.randomNumbers(4);
        System.out.println("验证码是"+phoneCode);
        //将验证码存入redis中
        String Rediskey="phone-"+phone;
        stringRedisTemplate.opsForValue().set(Rediskey,phoneCode,5, TimeUnit.MINUTES);
        //使用工具类发送短信
        MobileMessageUtil.sendMobileMessage(phone,phoneCode);
        return R.OK();

    }

    @Override
    public R phonelogin(LoginInfoVo loginInfoVo) {
        //手机号密码登录
        TbUser tbUser = tbUserMapper.selectOne(new QueryWrapper<TbUser>().lambda().eq(TbUser::getPhone, loginInfoVo.getUserMobile()).eq(TbUser::getPassWord, loginInfoVo.getUserPass()));
        if (tbUser==null){
            return R.ERROR(500,"手机号或密码错误");
        }
        if(tbUser.getRoleId()!=1){
            return R.ERROR(500,"该用户没用权限");
        }
        return R.OK(tbUser);
    }

    @Override
    public R getUserList(PageInfoVo pageInfoVo) {
        Page<TbUser> page = new Page<>(pageInfoVo.getPageNum(), pageInfoVo.getPageSize());
        Page<TbUser> tbUserPage = tbUserMapper.selectPage(page, null);
        return R.OK(tbUserPage);
    }


    @Override
    public R login(LoginInfoVo loginInfoVo) {
        TbUser tbUser = tbUserMapper.selectOne(new QueryWrapper<TbUser>().lambda().eq(TbUser::getPhone, loginInfoVo.getUserMobile()));
        if (tbUser==null){
            return R.ERROR(500,"手机号不存在");
        }
        if(tbUser.getRoleId()!=1){
            return R.ERROR(500,"该用户没用权限");
        }
        //判断手机验证码是否正确
        String Rediskey="phone-"+loginInfoVo.getUserMobile();
        String phoneCode = stringRedisTemplate.opsForValue().get(Rediskey);
        if (phoneCode==null){
            return R.ERROR(500,"验证码不存在");
        }
        if (!loginInfoVo.getUserCode().equals(phoneCode)){
            return R.ERROR(500,"验证码错误");
        }
        //生成token
        String token = Jwts.builder().signWith(SignatureAlgorithm.HS256, "123456")
                .claim("id", tbUser.getUserId())
                .claim("username", tbUser.getUserName())
                .compact();
        String tokenKey="token-"+tbUser.getUserId();
        stringRedisTemplate.opsForValue().set(tokenKey,token,30, TimeUnit.MINUTES);
        //返回token
        tbUser.setPassWord(null);
        tbUser.setToken(token);
        return R.OK(tbUser);

    }



}




